package com.ljb.smartinspector;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.sensoro.beacon.kit.Beacon;
import com.sensoro.beacon.kit.SensoroBeaconConnectionV4;
import com.sensoro.beacon.kit.connection.BeaconConfiguration;
import com.sensoro.beacon.kit.constants.AdvertisingInterval;
import com.sensoro.beacon.kit.constants.TransmitPower;

/**
 * Created by lijianbin on 2017/3/24.
 */

public class BeaconSettingActivity extends AppCompatActivity {
    private static final String TAG = "BeaconSettingActivity";
    private TextView tv, tv2;
    private Beacon iBeacon;
    private SensoroBeaconConnectionV4 connectionV4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        initData();

    }

    public void setting(View v) {
        initConnection();
    }

    public void disconnect(View v) {
        connectionV4.disconnect();
    }

    private void initConfig(SensoroBeaconConnectionV4 connectionV4) {
        BeaconConfiguration build = new BeaconConfiguration.Builder()
                .setTemperatureSamplingInterval(0)//设置温度采集频率
                .setTransmiPower(TransmitPower.LEVEL5)//设置广播频率
                .setAdvertisingInterval(AdvertisingInterval.ADVERTISING_INTERVAL_417_5)//设置广播间隔
                .build();
        connectionV4.writeBeaconConfiguration(build, new SensoroBeaconConnectionV4.WriteCallback() {
            @Override
            public void onWriteSuccess() {
                Log.e(TAG, "onWriteSuccess: 设置成功");
            }

            @Override
            public void onWriteFailure(int i) {
                Log.e(TAG, "onWriteFailure: 设置失败");
            }
        });
    }

    //初始化连接
    private void initConnection() {
        connectionV4 = new SensoroBeaconConnectionV4(this, iBeacon);
        try {
            connectionV4.connect(null, new SensoroBeaconConnectionV4.BeaconConnectionCallback() {
                @Override
                public void onConnectedSuccess(Beacon beacon) {
                    Log.e(TAG, "onConnectedSuccess: iBeacon连接成功");
                    initConfig(connectionV4);
                }

                @Override
                public void onConnectedFailure(int i) {
                    Log.e(TAG, "onConnectedSuccess: iBeacon连接失败");
                }

                @Override
                public void onDisconnected() {
                    Log.e(TAG, "onConnectedSuccess: iBeacon连接已断开");
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData() {
        Intent intent = getIntent();
        String iBeaconSN = intent.getStringExtra("iBeaconSN");
        iBeacon = intent.getParcelableExtra("iBeacon");

        tv.setText(iBeaconSN);
        tv2.setText(iBeacon.getSerialNumber());
    }

    private void initView() {
        tv = (TextView) findViewById(R.id.tv);
        tv2 = (TextView) findViewById(R.id.tv2);
    }
}
